﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HelperPrototype.TabPageControls;

namespace HelperPrototype
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            tabControl1.TabPages.Clear();

            // check login
            if (Program.MaNguoiDungHienTai == null)
            {
                //LoginForm f = new LoginForm();
                //f.ShowDialog();
                this.Close();
            }

            // ok, update status
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.NguoiDung u = dc.NguoiDungs.Single(x => x.MaNguoiDung == Program.MaNguoiDungHienTai);
            toolStripStatusLabel1.Text = "Người dùng: " + u.Ten;
            if (u.PhongBan != null)
                toolStripStatusLabel1.Text += ", phòng ban: " + u.PhongBan.TenPhongBan;

            // check quyen han
            TreeView [] treeViews = {treeView1, treeView2, treeView3,
                                     treeView4, treeView5, treeView6, treeView7};
            System.Collections.Generic.Stack<TreeNode> treeNodes = 
                new System.Collections.Generic.Stack<TreeNode>();
            foreach (TreeView tv in treeViews)
                foreach (TreeNode tn in tv.Nodes)
                    treeNodes.Push(tn);
            if (!Program.NguoiDungHienTai.isSuperAdmin())
            while (treeNodes.Count > 0)
            {
                TreeNode nd = treeNodes.Pop();
                foreach (TreeNode x in nd.Nodes)
                    treeNodes.Push(x);

                //nd.Remove();
                try
                {
                    int quyenID = int.Parse(((string)nd.Tag).Split(',').First());
                    int c = u.NhomNguoiDung.NhomNguoiDung_QuyenHans.Count(x => x.QuyenHanID == quyenID);
                    if (c == 0)
                        nd.Remove();
                }
                catch (System.Exception ex)
                {
                    nd.Remove();
                }
            }

            foreach (TreeView tv in treeViews)
            if (tv.Nodes.Count == 0)
            {
                tv.Visible = false;
                tv.Parent.Visible = false;
            }


        }

        private UserControl autoSizeGridInControl(UserControl userControl)
        {
            // autosize grid
            System.Collections.Generic.Stack<Control> cs = new System.Collections.Generic.Stack<Control>();
            cs.Push(userControl);

            while (cs.Count > 0)
            {
                Control uc = cs.Pop();

                foreach (Control c in uc.Controls)
                {
                    if (c.GetType() == typeof(DataGridView))
                    {
                        DataGridView dgv = (DataGridView)c;
                        dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
                        foreach (DataGridViewColumn cc in dgv.Columns)
                            cc.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                    }
                    else
                        cs.Push(c);
                }
            }

            return userControl;
        }

        public void addNewTab(String tabName, UserControl userControl)
        {
            
            // check old tab
            foreach (TabPage tpage in tabControl1.TabPages)
            if (tpage.Controls.Count > 0)
            {
                Type t = tpage.Controls[0].GetType();
                if (t == userControl.GetType())
                {
                    tabControl1.SelectedTab = tpage;
                    return;
                }
            }

            // create new tabpage
            TabPage tabPage = new TabPage(tabName);
            userControl.Tag = this;
            tabPage.UseVisualStyleBackColor = true;
            tabPage.Controls.Add(userControl);
            userControl.Dock = DockStyle.Fill;
            
            // add & go to new tabpage
            tabControl1.TabPages.Insert(tabControl1.SelectedIndex + 1, tabPage);
            tabControl1.SelectedTab = tabPage;
        }


        public void closeTab()
        {
            if (tabControl1.SelectedTab == null)
                return;

            int i = tabControl1.SelectedIndex;
            tabControl1.SelectedIndex = (tabControl1.TabCount >= i + 2) ? i + 1 : i - 1;
            tabControl1.TabPages.Remove(tabControl1.TabPages[i]);
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            // check
            TreeView tree = (TreeView) sender;
            if (tree.SelectedNode == null || tree.SelectedNode.Tag == null)
                return;

            // get tabpage class name
            String name = ((string)tree.SelectedNode.Tag).Split(',').Last();
            if (name == null)
                return;

            // go
            Type t = Type.GetType("HelperPrototype." + name);
            if (t != null)
            {
                UserControl u = (UserControl) Activator.CreateInstance(t);
                autoSizeGridInControl(u);
                if (u != null)
                    addNewTab(tree.SelectedNode.Text, u);
                else
                    MessageBox.Show("Can create user control " + name);
            }
            else
                MessageBox.Show("There're no " + name);

        }

        private void settingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OtherForms.SettingForm f = new OtherForms.SettingForm();
            f.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void đăngXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.MaNguoiDungHienTai = null;
            this.Close();
        }

        private void btnCloseTab_Click(object sender, EventArgs e)
        {
            closeTab();
        }


        private void treeCollapseBtn_Click(object sender, EventArgs e)
        {
            splitContainer1.SplitterDistance = (splitContainer1.SplitterDistance > 30) ? 30 : 239;
            treeCollapseBtn.Text = (splitContainer1.SplitterDistance > 30) ? "<" : ">";
        }

        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            /*
            skinEngine1.RemoveForm(this, true);
            skinEngine1.Active = false;
            skinEngine1.Dispose();
             */
        }

        private void refreshBtn_Click(object sender, EventArgs e)
        {
            // check
            TabPage tabPage = tabControl1.SelectedTab;
            if (tabPage == null)
                return;

            // replace new one
            Control uc = tabPage.Controls[0];
            UserControl newOne = (UserControl)Activator.CreateInstance(uc.GetType());
            autoSizeGridInControl(newOne);
            tabPage.Controls.Remove(uc);
            tabPage.Controls.Add(newOne);
            newOne.Dock = DockStyle.Fill;
        }

        /////////////////////////////////////
        // skin
        /////////////////////////////////////

        private void diamondBlueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(0);
        }

        private void diamondRedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(1);
        }

        private void macOSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(2);
        }

        private void mediaPlayer10ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(3);
        }

        private void office2007ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(4);
        }

        private void sportGreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(5);
        }

        private void steelBlackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(6);
        }

        private void vista2Color4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(7);
        }

        private void waveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(8);
        }


        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox2 f = new AboutBox2();
            f.ShowDialog();
        }


    }
}
